/**
 * Created by teddy on 6/30/16.
 */

var mobileStatus = 0; // 1 is using...

$(function () {
    /*Job Table double click event*/
    $('#JobTable').on('dbl-click-row.bs.table', function (e, row, $element) {
        if(row['failCount'] > 0) {
            window.open("AppUICaseList.php?jobid=" + row["id"] + "&status=0");
        }
        else {
            window.open("AppUICaseList.php?jobid=" + row["id"]+ "&status=1")
        }
    })

    /*Check Mobile status*/
    checkstatus();

});

/*Run Job*/
function goRun() {

    $("#MessageText").html("");

    /*Invoke PHP script*/
    /*Ajax Call*/
    $.ajax({
        type: "Get",
        url: "iosAppium/Run.php",
        beforeSend:LoadGIF(),
        success: function(data) {
            /*Success*/
            if(data == 0) {
                var htmlString = "<div style=\"height: auto; padding: 8px;margin:0px;border: 0px;border-radius: 5px;\" class=\"alert alert-success\" role=\"alert\"><strong>Well Done!</strong></div>";
                $("#LoadingDiv").html("");
                $("#MessageText").html(htmlString).hide().fadeIn(1000);
                /*Refresh Job Table*/
                $('#JobTable').bootstrapTable('refresh');

            }
            /*Fail*/
            else {
                var htmlString = "<div style=\"height: auto; padding: 8px;margin:0px;border: 0px;border-radius: 5px;\" class=\"alert alert-danger\" role=\"alert\"><strong>Error in RUN!</strong></div>";
                $("#LoadingDiv").html("");
                $("#MessageText").html(htmlString);
            }
        }
    });
}

// Check Mobile status
function checkstatus() {
    $.ajax({
        type: "Get",
        url: "checkStatus.php",
        success: function(data) {

            /*Not Using*/
            if(data == 0) {

            }

            else {
                LoadGIF();
                var htmlString = "<div style=\"height: auto; padding: 8px;margin:0px;border: 0px;border-radius: 5px;\" class=\"alert alert-danger\" role=\"alert\"><strong>设备只有1台,正在运行中...</strong></div>";
                $("#MessageText").html(htmlString);
            }
        }
    });
}

/*Load GIF*/
function LoadGIF() {
    $('#RunBtn').attr("disabled", true);
    var htmlString = "<i class=\"icon-spinner icon-spin icon-2x pull-left\"></i>";
    $("#LoadingDiv").html(htmlString);
}

/*Row Styles For Job Table*/
function rowStyle(row, index) {

    /*define five type*/
    var classes = ['active', 'success', 'info', 'warning', 'danger'];

    /*if has error*/
    if (row["failCount"] > 0) {
        return {
            classes: classes[4]
        };
    }
    /*if no error*/
    else if (row["failCount"] == 0)
    {
        return {
            classes: classes[1]
        };
    }
    /*for old data --- no fail column*/
    else if (row["failCount"] == "" || row["failCount"] == null)
    {
        return{
            classes: classes[3]
        }
    }
    /*default*/
    return {};
}